package api

import (
	"gin_test/global"
	"gin_test/model"
	"gin_test/result"
	"gin_test/utils"
	"github.com/gin-gonic/gin"
	"time"
)

// 创建用户
func CreateUser(c *gin.Context) {
	// 参数绑定
	var dto model.AddUser
	_ = c.BindJSON(&dto)
	//查询邮箱是否存在，不能重复
	userInfo := model.GetUserByEmail(dto.Email)
	if userInfo.ID != 0 {
		global.Log.Infoln("user exists", userInfo)
		result.Failed(c, result.CodeUserExist, "", nil)
		return
	}
	user := model.UserModel{
		Email:     dto.Email,
		UserName:  dto.UserName,
		CreatedAt: utils.HTime{time.Now()},
	}
	err := model.CreateUser(&user)
	if err != nil {
		global.Log.Errorln("create user error", err.Error())
		result.Failed(c, result.CodeFailed, "", nil)
		return
	}
	returnData := make(map[string]interface{})
	returnData["user_id"] = user.ID
	result.Success(c, returnData)
}
